package com.limox.bishe.viewgroup;

import com.limox.bishe.R;
import com.limox.bishe.activity.LoginActivity;
import com.limox.bishe.db.DataBaseManager;
import com.limox.bishe.viewgroup.ClassroomLeft.OnListItemClickListenner;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.textservice.TextInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.TextView;
import android.widget.Toast;

public class Classroom extends Fragment {

	private String classroom;
	private String courname;
	private String teacher;
	private EditText edittextNote;
	private EditText edittextTitle;
	private Button butAdd;
	private SQLiteDatabase SQLdb;
	private TextView textID;
	private Button butNew;
	OnRefesh onRefesh;
	
	
	//定义一个接口
	public interface OnRefesh{
		public void reflesh();
	}

	public Classroom() {

	}

	public Classroom(Bundle bundle) {
	}

	public Classroom(String classroom, String courname, String teacher) {
		this.classroom = classroom;
		this.courname = courname;
		this.teacher = teacher;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setRetainInstance(true);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.act_classroom, null);
		edittextNote = (EditText) view.findViewById(R.id.edit_note);
		edittextTitle = (EditText) view.findViewById(R.id.edit_title);
		textID = (TextView) view.findViewById(R.id.text_noteid);
		butAdd = (Button) view.findViewById(R.id.but_add);
		butNew=(Button)view.findViewById(R.id.but_new);
		butAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (edittextNote.getText().toString().trim().equals("")
						|| edittextTitle.getText().toString().trim().equals("")) {
					Toast.makeText(getActivity(), "校长提示：标题/内容有空，请多写点知识吧", 0)
							.show();
				} else {
					SQLdb = DataBaseManager
							.OpenDb(getActivity(), "kb", null, 3); 
					// 新建的笔记
					if (textID.getText().toString().equals("")) {
						// 将笔记保存在数据库
						DataBaseManager.InsertTb_note(SQLdb, edittextTitle
								.getText().toString(), edittextNote.getText()
								.toString(), courname);
						Toast.makeText(getActivity(), "已保存新笔记", 0).show();

						// 查询新保存笔记的id
						Cursor cursor = SQLdb
								.rawQuery(
										"select last_insert_rowid() from my_note",
										null);
						if (cursor.moveToFirst())
							textID.setText(String.valueOf(cursor.getInt(0)));

					} else {
						// 修改笔记
						// 修改笔记，根据id修改数据库信息
						DataBaseManager.UpdateNote(SQLdb, edittextTitle
								.getText().toString(), edittextNote.getText()
								.toString(), textID.getText().toString());
						Toast.makeText(getActivity(), "已保存笔记", 0).show();
					   
					}
					//刷新左边抽屉的列表
					onRefesh.reflesh(); 
					butNew.setVisibility(View.VISIBLE);
				}

			}
		});
		
		
		//清除内容
		butNew.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textID.setText("");
				edittextNote.setText("");
				edittextTitle.setText("");
				butNew.setVisibility(View.GONE);				
			}
		});

		return view;
	}

	public void setEdittext(String note) {
		edittextNote.setText(note);

	}

	public void setTitle(String title) {
		edittextTitle.setText(title);
	}

	public void setId(String id) {
		textID.setText(id);
		butNew.setVisibility(View.VISIBLE);
	}
	
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		onRefesh = (OnRefesh) activity;
	}

	
	
}
